package com.shixun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.shixun.domain.Checkdorm;
import com.shixun.domain.Student;
import com.shixun.dto.CheckdormDto;
import com.shixun.service.CheckdormService;
import com.shixun.service.StudentService;
import com.shixun.utils.R;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.Date;

@RestController
@RequestMapping("/checkdorm")
public class CheckdormController {
    @Autowired
    CheckdormService checkdormService;

    @Autowired
    private StudentService studentService;

    /**
     * 管理员查询所有学生的查寝信息
     * @param pageSize
     * @param pageNum
     * @param beginTime
     * @param endTime
     * @param checkState
     * @return
     */
    @GetMapping("/admin/page")
    public R checkDormPage(int pageSize, int pageNum, String sname, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
                           @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime, String checkState) {
        R r = checkdormService.checkDormPage(pageSize, pageNum,sname,beginTime, endTime, checkState);
        return r;
    }

    /**
     * 学生查看自己的历史查寝信息
     * @param pageSize
     * @param pageNum
     * @param sid
     * @param checkState
     * @return
     */
    @GetMapping("/student/page")
    public R StudentCheckDormPage(int pageSize, int pageNum, int sid, String checkState) {
        Page<Checkdorm> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Checkdorm> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Checkdorm::getSid,sid);
        queryWrapper.eq(StringUtils.isNotEmpty(checkState),Checkdorm::getCheckState,checkState);
        Page<Checkdorm> page1 = checkdormService.page(page, queryWrapper);
        return new R(200,"",page1);
    }

    /**
     * 助手点名功能
     * @param checkdorm
     * @return
     */
    @PostMapping("/addCheck")
    public R addCheck(@RequestBody Checkdorm checkdorm){
        if (checkdorm.getCheckState()==null){
            return new R(500,"状态不能为空",null);
        }
        /**
         * 判断当天是否有查寝，同一天不能连续查
         */
        Integer l=checkdormService.checkDay(checkdorm);

        if (l==0){//没查过
            checkdorm.setCheckTime(LocalDateTime.now());
            checkdormService.save(checkdorm);
            return new R(200,"查寝成功",null);
        }
        return new R(500,"今天已查过该学生",null);

    }

    /**
     * 点名前要列出所有没请假的学生
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/checkStudent")
    public R checkStudent(int pageSize, int pageNum){
       R r= checkdormService.checkStudent(pageSize,pageNum);
       return r;
    }
    @DeleteMapping("/del/{id}")
    public R deleteCheckDorm(@PathVariable Long id){
        checkdormService.removeById(id);
        return new R(200,"删除成功",null);
    }
    /**
     * 获得session,得到登录学生的id
     */
    @GetMapping("/getSession")
    public R getSession(HttpSession session){
        String sno = (String)session.getAttribute("student");
        if (sno==null){
            return new R(500,"请先登录",null);
        }
        LambdaQueryWrapper<Student> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getSno,sno);
        Student one = studentService.getOne(queryWrapper);
        Long id = one.getId();

            return new R(200,"",id);
    }

}
